<?php
	include_once("../Business/survey_service.php");
	include_once("../Business/survey_question_service.php");
	include_once("../Business/survey_answer_service.php");
	
	include_once("../Business/Data/Entity/survey.php");
	include_once("../Business/Data/Entity/survey_question.php");
	include_once("../Business/Data/Entity/survey_answer.php");
		
	include_once("uitilities.php");
	
	$msgstatus = 'display:none';
	$msg = '';
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$s = new survey(0, post("name"), 0);
		$sid = survey_service::insert_survey($s);
		
		$nq = post("nq");
		for($i=1 ; $i<=$nq ; $i++) {
			$sq = new survey_question(0, $sid, post("q".$i), 0);
			$sqid = survey_question_service::insert_survey_question($sq);
			
			$na = post("naq".$i);
			for($j=1 ; $j<=$na ; $j++) {
				$sa = new survey_answer(0, $sqid, post("q".$i."a".$j), post("q".$i."m".$j));
				survey_answer_service::insert_survey_answer($sa);
			}
		}
	}
	
	$main_content = '
<form method="post">
<center>
<p class="label-submit">
	<input value="Add" type="submit" >
</p>
</center>
<h3>Survey</h3>	
    <table class="survey">
        <tbody>
            <tr class="even">
                <td>
                    <dl>
                        <dt>Name</dt>
                    </dl>
                </td>
                <td>
                    <input type="text" name="name" value="" />                
                </td>
            </tr>
        </tbody>
    </table>
<h3>Questions (<span id="nQuestionValue">0</span>)<input title="Add a question" value="+q" type="button" onclick="addQuestion()"></h3>	
	<input type="hidden" name="nq" value="0" />
	<div id="questions">
	</div>
	<center>
	<p class="label-submit">
		<input value="Add" type="submit" >
	</p>
	</center>
	
</form>
<script>
	function updateQuestionInfo() {
		$("dl[name=q]").each(function(index) {
			$(this).find("span[id=qTitle]").html("q" + (index + 1));
			$(this).find("textarea[id=qContent]").attr("name" , "q" + (index + 1));
			
			if(index % 2 == 1) $(this).addClass("grey");
			else $(this).removeClass("grey");
		});
		
		var count = $("dl[name=q]").length;
		$("#nQuestionValue").html(count ? count : "0") ;
		$("input[name=nq]").val(count ? count : "0") ;
		
	}
	
	function updateAnswerInfo(dlNameQ) {
		var qID = dlNameQ.find("textarea[id=qContent]").attr("name");	
		dlNameQ.find("dl[name=a]").each(function(index) {
			$(this).find("span[id=aTitle]").html("a" + (index + 1));
			
			$(this).find("textarea[id=aContent]").attr("name" , qID + "a" + (index + 1));
			$(this).find("input[id=aMark]").attr("name" , qID + "m" + (index + 1));
			
		});
		
		var count = dlNameQ.find("dl[name=a]").length;
		dlNameQ.find("span[id=nAnswerValue]").html(count ? count : "0") ;
		dlNameQ.find("input[id=na]").val(count ? count : "0") ;
		dlNameQ.find("input[id=na]").attr("name", "na" + qID);
		
	}
	
	function addQuestion(){
		$("#questions").append(
	"	<dl name=\"q\" class=\"question\"> \
			<dt>\
				<input title=\"Delete this question\" value=\"-\" type=\"button\" onclick=\"deleteQuestion(this)\">  \
				<input title=\"Add an answer\" value=\"+a\" type=\"button\" onclick=\"addAnswer(this)\"> \
				<span id=\"qTitle\" /> \
				(<span id=\"nAnswerValue\">0</span>) \
				<textarea title=\"Question content\" id=\"qContent\" type=\"text\"></textarea> \
				<input type=\"hidden\" id=\"na\" value=\"0\" /></dt> \
			<dd class=\"answer\" />\
		</dl> \
		");
		updateQuestionInfo();
	}
	
	function deleteQuestion(obj){
		$(obj).parent().parent().remove();
		updateQuestionInfo();
	}
	
	function addAnswer(obj){
		$(obj).parent().next().append(
	"	<dl name=\"a\" > \
			<dt><input title=\"Delete this answer\" value=\"-\" type=\"button\" onclick=\"deleteAnswer(this)\"> \
				<span id=\"aTitle\" />\
				<input title=\"Mark of answer\" id=\"aMark\" type=\"text\" class=\"mark\" value=\"0\">\
				<textarea title=\"Answer content\" id=\"aContent\" type=\"text\" ></textarea>\
			</dt> \
			<dd></dd> \
		</dl> \
		");
		updateAnswerInfo($(obj).parent().parent());
	}
	
	function deleteAnswer(obj){
		var dlNameQ = $(obj).parent().parent().parent().parent();
		$(obj).parent().parent().remove();
		updateAnswerInfo(dlNameQ);
	}

</script>
';

	include_once("masters/pages.php");
?> 